package com.whit.nuoge.dao.impl;


import com.whit.nuoge.dao.UsergroupDao;
import com.whit.nuoge.pojo.Usergroup;

import java.util.List;

public class UsergroupDaoImpl extends BaseDao implements UsergroupDao {
    @Override
    public int save(Usergroup usergroup) {
        String sql = "insert into t_usergroup(`groupname`,`memo`,`phone`) values(?,?,?)";
        return update(sql, usergroup.getGroupname(), usergroup.getMemo(), usergroup.getPhone());
    }

    @Override
    public int update(Usergroup usergroup) {
        String sql = "update t_usergroup set `groupname`=?,`memo`=?,`phone`=? where id=?";
        return update(sql,usergroup.getGroupname(), usergroup.getMemo(), usergroup.getPhone(),usergroup.getId());
    }

    @Override
    public int deleteById(Integer id) {
        String sql = "delete from t_usergroup where id = ?";
        return update(sql,id);
    }

    @Override
    public List<Usergroup> query() {
        String sql = "select `groupname`,`memo`,`phone`,`id` from t_usergroup";
        return queryList(Usergroup.class,sql);
    }

    @Override
    public Usergroup queryById(Integer id) {
        String sql = "select `groupname`,`memo`,`phone`,`id` from t_usergroup where id = ?";
        return queryOne(Usergroup.class,sql,id);
    }

    @Override
    public Integer queryForPageCount(String queryString) {
        String sql = "";
        Object count = null;
        if(queryString == null || queryString == ""){
            sql = "select count(*) from t_usergroup ";
            count = queryForSingleValue(sql);
        }else{
            queryString = "%" + queryString + "%";
            sql = "select count(*) from t_usergroup where groupname like ? or memo like ? or phone like ?";
            count = queryForSingleValue(sql,queryString,queryString,queryString);
        }
        return new Integer(count.toString());
    }

    @Override
    public List<Usergroup> queryForPageItems(Integer begin, Integer pageSize,String queryString) {
        String sql = null;
        if(queryString == null || queryString == ""){
            sql =  "select `groupname`,`memo`,`phone`,`id` " +
                    "from t_usergroup  limit ? , ?";
            return queryList(Usergroup.class,sql,begin,pageSize);
        }else{
            queryString = "%" + queryString + "%";
            sql =  "select `groupname`,`memo`,`phone`,`id` " +
                    "from t_usergroup where groupname like ? or memo like ? or phone like ?  limit ? , ?";
            return queryList(Usergroup.class,sql,queryString,queryString,queryString,begin,pageSize);
        }
    }

    @Override
    public Integer queryForPageCountByQueryString(String queryString) {
        String sql = "";
        Object count = null;
        if(queryString == null || queryString == ""){
            sql = "select count(*) from t_usergroup ";
            count = queryForSingleValue(sql);
        }else{
            queryString = "%" + queryString + "%";
            sql = "select count(*) from t_usergroup where groupname like ? or memo like ? or phone like ? ";
            count = queryForSingleValue(sql,queryString,queryString,queryString);
        }


        return new Integer(count.toString());
    }

    @Override
    public List<Usergroup> queryForPageItemsByQueryString(Integer begin, Integer pageSize, String queryString) {
        String sql = null;
        if(queryString == null || queryString == ""){
            sql =  "select `groupname`,`memo`,`phone`,`id` " +
                    "from t_usergroup  limit ? , ?";
            return queryList(Usergroup.class,sql,begin,pageSize);

        }else{
            queryString = "%" + queryString + "%";
            sql =  "select `groupname`,`memo`,`phone`,`id` " +
                    "from t_usergroup where groupname like ? or memo like ? or phone like ? limit ? , ?";
            return queryList(Usergroup.class,sql,queryString,queryString,queryString,begin,pageSize);
        }
    }
}
